package thisgame.model;


public class Rect {
	// 불변식: left <= right, top <= bottom
	private int left, top, right, bottom;
	
	public Rect(int left, int top, int right, int bottom) {
		this.setLeft(left);
		this.setTop(top);
		this.setRight(right);
		this.setBottom(bottom);
	}
	public Rect() {
		this(0, 0, 0, 0);
	}
	
	
	// 어마어마한 양의 getters/setters
	public void setLeft(int newLeft) {
		if (newLeft <= right) {
			left = newLeft;			
		} else {
			left = right;
			right = newLeft;
		}
	}
	public int getLeft() {
		return left;
	}
	public void setRight(int newRight) {
		if (left <= newRight) {
			right = newRight;			
		} else {
			right = left;
			left = newRight;
		}
	}
	public int getRight() {
		return right;
	}
	public void setTop(int newTop) {
		if (newTop <= bottom) {
			top = newTop;
		} else {
			top = bottom;
			bottom = newTop;
		}
	}
	public int getTop() {
		return top;
	}
	public void setBottom(int newBottom) {
		if (top <= newBottom) {
			bottom = newBottom;
		} else {
			bottom = top;
			top = newBottom;
		}
	}
	public int getBottom() {
		return bottom;
	}
	public void setX(int newX) {
		int width = getWidth();
		this.left = newX;
		setWidth(width);
	}
	public int getX() {
		return getLeft();
	}
	public void setY(int newY) {
		int height = getHeight();
		this.top = newY;
		setHeight(height);
	}
	public int getY() {
		return getTop();
	}
	public void setWidth(int newWidth) {
		setRight(getLeft() + newWidth);
	}
	public int getWidth() {
		return getRight() - getLeft();
	}
	public void setHeight(int newHeight) {
		setBottom(getTop() + newHeight);
	}
	public int getHeight() {
		return getBottom() - getTop();
	}
	
	public boolean contains(Position pos) {
		if (getLeft() <= pos.getX() && pos.getX() <= getRight()
				&& getTop() <= pos.getY() && pos.getY() <= getBottom()) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean equals(Object obj) {
		if (obj instanceof Rect) {
			Rect other = (Rect)obj;
			return getLeft() == other.getLeft()
				&& getTop() == other.getTop()
				&& getRight() == other.getRight()
				&& getBottom() == other.getBottom();
		} else {
			return false;
		}
	}
}
